
#import "JddNetDataCommunication.h"
#import "JddCommonFunction.h"
#import "JddError.h"

@implementation JddNetDataCommunication

//-(NSData*)requestDataByPostUrl:(NSString*)url bodyString:(NSString*)bodyString{
//	NSLog(@"<NetDataCommunication>-requestDataByPostUrl JSon Request:\t%@", bodyString);
//	
//    NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
//    
//    return [self requestDataByPostUrl:url bodyData:bodyData];
//}

-(NSData*)requestDataByPostBodyData:(NSData*)bodyData{
   return [self requestDataByPostUrl:BASE_SERVER_URL bodyData:bodyData];
}

//2014.7.9 zbj add
//购彩专用
-(NSData*)requestDataByPostBodyDataGouCai:(NSData*)bodyData{
    return [self requestDataByPostUrl:BASE_SERVER_URL_GOUCAI bodyData:bodyData];
}




-(NSData*)requestDataByPostUrl:(NSString*)url bodyData:(NSData*)bodyData{
    if ([JddCommonFunction checkNetWork]) {
    NSString *encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:TIMEOUT_INTERVAL];
	
    [request setHTTPMethod:@"POST"];
	[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    
    [request setHTTPBody:bodyData];  
    [request setValue:[NSString stringWithFormat:@"%d", [bodyData length]] forHTTPHeaderField:@"Content-Length"];     
	[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];
	NSURLResponse *response = nil;
	NSError *error = nil;
	
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
        if (response == nil) {
//            [JddCommonFunction messageBoxWithMessage:@"您的网络不给力，请检查网络是否连接！"];
            NSLog(@"%@",@"您的网络不给力,请检查网络是否连接!");
        } 
        if (!error && response) {
            return dataReply;
        }
        else {
            NSLog(@"%@", [error localizedDescription]);
    //        [[SSGlobalParameter sharedSSGlobalParameter] setErrorMessage:error.localizedDescription];
            return nil;
        }	
	}else {
//        [JddCommonFunction messageBoxWithMessage:@"网络未连接，请检查网络设置!"];
//        [JddCommonFunction setWaitingViewHidden:NO fromController:[JddViewControllerManager sharedInstance].navigationController.topViewController];
        return nil;
    }
	
}

-(NSData*)requestDataByUrl:(NSString*)url{
    NSString *encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"<NetDataCommunication>-requestDataByUrl %@", encodedUrl);
    
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
	
	NSURLResponse *response = nil;
	NSError *error = nil;
	
	NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
	if (!error && response) {
		return dataReply;
	}
	else {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
            [JddError sharedInstance].message = error.localizedDescription;
        }
        else{
        
            [JddError sharedInstance].message = @"未知错误";
           }
                                	
//        [[SSGlobalParameter sharedSSGlobalParameter] setErrorMessage:error.localizedDescription];
	}
	
	return nil;
}

-(void)requestDataAsync:(NSString *)url selectorCallback:(SEL)callback objectReceiveCallback:(id)callbackObject{
	selectorCallback = callback;
	objectResponseCallback = callbackObject;

	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
	connection = [NSURLConnection connectionWithRequest:request delegate:self];
	if (connection) {
		[connection start];
	}
}

-(void)dealloc{
	[connection cancel];
}

@end
